package vdfX

// VDF is the struct holding necessary state for a hash chain delay function.
type VDF struct {
	difficulty int
	input      [32]byte
	stop       chan struct{}
	// output     [516]byte
	// outputChan chan [516]byte
	// finished   bool
}

//size of long integers in quadratic function group
const sizeInBits = 2048

// New create a new instance of VDF.
func New(difficulty int, input [32]byte) *VDF {
	return &VDF{
		difficulty: difficulty,
		input:      input,
		stop:       make(chan struct{}),
	}
}

// Eval runs the VDF, output include y and proof byte
// currently on i7-6700K, it takes about 14 seconds when iteration is set to 10000
func (vdf *VDF) Eval() [516]byte {
	yBuf, proofBuf := GenerateVDFWithStopChan(vdf.input[:], vdf.difficulty, sizeInBits, vdf.stop)
	var output [516]byte
	copy(output[:], yBuf)
	copy(output[258:], proofBuf)
	return output
}

// StopEval stop the compute function
// the eval will be stoped and output nil
func (vdf *VDF) StopEval() {
	vdf.stop <- struct{}{}
}

// Verify runs the verification of generated proof
// currently on i7-6700K, verification takes about 350 ms
func (vdf *VDF) Verify(proof [516]byte) bool {
	return VerifyVDF(vdf.input[:], proof[:], vdf.difficulty, sizeInBits)
}
